﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.DAL.Base;

namespace SalesSys.DAL.Models
{
    /*
     * 锁价单相关
     */
    public class LockOrder : DbEntityBase<Guid>
    {
        public LockOrder()
        {
            Orders = new List<Order>();
        }

        public class Mapper : SalMapper<LockOrder, Guid>
        {
            public Mapper()
            {
                HasMany(p => p.Records).WithOptional().WillCascadeOnDelete();
            }
        }
        /// <summary>
        /// 退款金额
        /// </summary>
        public decimal Refund { get; set; }
        /// <summary>
        /// 创建人，一般为客户专员
        /// </summary>
        public virtual Employee Creater { get; set; }

        /// <summary>
        /// 客户
        /// </summary>
        public virtual Customer Customer { get; set; }

        /// <summary>
        /// 标件户头
        /// </summary>
        public virtual Account Account { get; set; }

        /// <summary>
        /// 是否标件
        /// </summary>
        public bool IsStandard { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { get; set; }

        /// <summary>
        /// 仓库
        /// </summary>
        public virtual Provider Provider { get; set; }

        /// <summary>
        /// 是否商品锁价
        /// </summary>
        public bool IsGoodsLock { get; set; }

        /// <summary>
        /// 商品历史信息
        /// </summary>
        public virtual ICollection<DepotCommodityRecord> DepotCommodityRecords { get; set; }

        /// <summary>
        /// 流程状态
        /// </summary>
        public ProcessState ProcessState { get; set; }

        /// <summary>
        /// 每吨优惠合计,优惠使用负值表示
        /// </summary>
        public decimal UnitDiscount { get; set; }

        /// <summary>
        /// 订单状态
        /// </summary>
        public OrderType OrderType { get; set; }

        /// <summary>
        /// 总价
        /// </summary>
        public decimal Total { get; set; }

        /// <summary>
        /// 执行状态
        /// </summary>
        public OrderState State { get; set; }

        /// <summary>
        /// 是否审批中，审批流程中会包含某个计划单编号
        /// </summary>
        public bool IsApproval { get; set; }

        /// <summary>
        /// 是否暂停
        /// </summary>
        public bool IsPause { get; set; }

        /// <summary>
        /// 订单纪录
        /// </summary>
        public virtual ICollection<Order> Orders { get; set; }

        /// <summary>
        /// 锁价单变化记录
        /// </summary>
        public virtual ICollection<OrderRecord> Records { get; set; }

        /// <summary>
        /// 申请单价优惠金额,优惠使用负值表示
        /// </summary>
        public decimal ApplyDiscount { get; set; }

        /// <summary>
        /// 根据商品信息计算每吨优惠
        /// </summary>
        public void CalcUnitDiscount()
        {
            if (this.Provider == null)
            {
                UnitDiscount = 0;
            }
            else
            {
                if (!this.IsStandard)
                    this.UnitDiscount += this.Provider.FullPaymentDiscount + this.Provider.NonstandardDiscount;
            }
        }
        public decimal Surplus
        {
            get
            {
                return GetSurplus();

            }
        }
        /// <summary>
        /// 锁价单剩余金额
        /// </summary>
        /// <returns></returns>
        public decimal GetSurplus()
        {
            //锁价单总额上浮10%
            return (this.Total + this.Total * (decimal)0.1) - this.Orders.Where(p => p.State != OrderState.Cancel && p.State != OrderState.Refund && p.State != OrderState.Return).Sum(p => p.Total);
        }
    }
    ///// <summary>
    ///// 锁价单
    ///// </summary>

    //[Obsolete("暂不使用")]
    //public class LockPriceList : DbEntityBase<Guid>
    //{
    //    /// <summary>
    //    /// 是否商品锁价
    //    /// </summary>
    //    public bool IsGoodsLock { get; set; }
    //    public decimal LockPrice { get; set; }
    //    public decimal LockWeight { get; set; }
    //    public virtual Commodity LockGoods { get; set; }
    //    /// <summary>
    //    /// 剩余重量
    //    /// </summary>
    //    public decimal RestWeight { get; set; }

    //    public virtual Supplier Supplier { get; set; }

    //    public virtual Customer Customer { get; set; }
    //    public virtual Employee Create { get; set; }
    //    public DateTime CreateTime { get; set; }
    //    public ApprovalState ApprovalState { get; set; }

    //    public virtual ICollection<Order> OrderList { get; set; }
    //    public class Mapper : SalMapper<LockPriceList, Guid>
    //    {
    //        public Mapper()
    //        {
    //        }
    //    }
    //}
}
